package com.hrm.controller;

import com.hrm.dto.StarffPercentByDepartment;
import com.hrm.entity.Employee;
import com.hrm.dto.StaffPercentBySex;
import com.hrm.service.EmployeeService;
import com.hrm.utils.JsonMsg;
import com.hrm.utils.MySecurity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Auther:
 * @Date: 2020/5/20 15:23
 * @Description: 系统页面跳转 个人信息修改
 */
@Controller
@RequestMapping("/system")
public class SystemController extends BaseController {
    @Autowired
    EmployeeService employeeService;

    /**
     * 进入首页
     * @return String
     */
    @RequestMapping("/index.html")
    public String index(){
        return "index";
    }

    /**
     * 进入主页
     * @return String
     */
    @RequestMapping("/home.html")
    public String home(){
        return "home/home";
    }

    /**
     * 进入个人信息
     * @return: String
     */
    @RequestMapping("/info.html")
    public String info(Model model){
        Employee employee = (Employee) session.getAttribute("user");
        model.addAttribute("empInfo",employee);
       return "set/info";
    }

    /**
     * 进入密码修改页面
     * @return String
     */
    @RequestMapping("/password.html")
    public String password(){
        return "set/password";
    }

    /**
     * 进入邮箱修改页面
     * @return String
     */
    @RequestMapping("/email.html")
    public String email(){
        return "set/email";
    }

    /**
     * 权限不足提示错误页面
     * @return String
     */
    public String error(){
        return "error/error";
    }


    /**
     * 上传头像
     * @param file
     * @return JsonMsg
     */
    @RequestMapping("/uploadIcon.html")
    @ResponseBody
    public JsonMsg uploadIcon(@RequestParam(value = "file",required = false) MultipartFile file){
        Employee employee = (Employee) session.getAttribute("user");
        String relaFilePath = employeeService.uploadIcon(file,request,employee.getId());
        System.out.println(relaFilePath);
        return JsonMsg.success().addInfo("src",relaFilePath);
    }

    /**
     * 设置用户信息
     * @param employee
     * @return JsonMsg
     */
    @RequestMapping("/setInfo.html")
    @ResponseBody
    public JsonMsg setInfo(Employee employee){
        if(employeeService.setInfo(employee)){
            employeeService.updateSession(session,employee.getId());
            return JsonMsg.success();
        }else return JsonMsg.fail().addInfo("SET_ERROR","非法修改操作!");
    }

    /**
     * 验证码验证
     * @param code
     * @return JsonMsg
     */
    @RequestMapping("/emailCodeTest.html")
    @ResponseBody
    public JsonMsg emailCodeTest(@RequestParam(value = "code") String code){
        if(employeeService.emailCodeTest(code,session)){
            return JsonMsg.success();
        }else return JsonMsg.fail().addInfo("TEST_ERROR","验证码错误!");
    }

    /**
     * 重置密码(根据旧密码)
     * @param oldPwd
     * @param newPassword
     * @return JsonMsg
     */
    @RequestMapping("/resetPwd.html")
    @ResponseBody
    public JsonMsg modifyPwd(@RequestParam(value = "oldPassword")String oldPwd,@RequestParam(value = "newPassword")String newPassword){
        Employee employee = (Employee) session.getAttribute("user");
        if(!(MySecurity.encryptUserPassword(oldPwd,"5").equals(employee.getPassword()))){
            return JsonMsg.fail().addInfo("MODIFY_ERROR","原密码错误!");
        }else {
            employee.setPassword(newPassword);
            employeeService.updateSession(session,employee.getId());
            employeeService.resetPwd(employee);
            return JsonMsg.success();
        }
    }

    /**
     * 更换绑定邮箱
     * @param newEmail
     * @return JsonMsg
     */
    @RequestMapping("/resetEmail.html")
    @ResponseBody
    public JsonMsg modifyEmail(@RequestParam(value = "newEmail")String newEmail){
        Employee employee = (Employee) session.getAttribute("user");
        employee.setEmail(newEmail);
        if(employeeService.checkEmail(employee)){
            employeeService.updateSession(session,employee.getId());
            employeeService.modifyEmail(employee);
            return JsonMsg.success();
        }else return JsonMsg.fail().addInfo("MODIFY_ERROR","新邮箱已存在!");
    }


    /**
     * 获取公司不同性别人数
     * @return JsonMsg
     */
    @RequestMapping("/findSexPercent.html")
    @ResponseBody
    public JsonMsg findSexPercent() {
        StaffPercentBySex staffPercentBySex = employeeService.staffPercent();
        return JsonMsg.success().addInfo("STAFF_PERCENT",staffPercentBySex);
    }


    /**
     * 获取各部门人数
     * @return JsonMsg
     */
    @RequestMapping("/findPersonByDepartment.html")
    @ResponseBody
    public JsonMsg findPersonByDepartment(){
       List<StarffPercentByDepartment> starffPercentByDepartmentList = employeeService.staffPercentByDepartment();
        return JsonMsg.success().addInfo("starffPercentByDepartmentList",starffPercentByDepartmentList);
    }


}
